package cn.itcast.controller;

import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import cn.itcast.utils.PinYin4jUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//区域控制类
@Controller
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 地区excel一键导入
     * @return
     */
    @ResponseBody
    @RequestMapping("/area_batchImport")
    public String areaImport(MultipartFile file) throws IOException {

        //解析excel文件
        //创建一个book对象装载excel文件
        HSSFWorkbook workbook =new HSSFWorkbook(file.getInputStream());

        //获取第一页
        HSSFSheet sheetAt = workbook.getSheetAt(0);

        List<Area> areas =new ArrayList<>();

        //获取行 (去掉第一行)
        for (Row row : sheetAt) {
            if(row.getRowNum()==0){
                //第一行 跳过
                continue;
            }
                                                            //获取列的值是否为空
            if(row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
                //跳过空格列
                continue;
            }

            //创建地域的对象
            Area area=new Area();

            String quyuindex = row.getCell(0).getStringCellValue();
            String shengfen = row.getCell(1).getStringCellValue();
            String chengshi = row.getCell(2).getStringCellValue();
            String quyu = row.getCell(3).getStringCellValue();
            String youbian = row.getCell(4).getStringCellValue();

            area.setId(quyuindex);  //区域编号
            area.setProvince(shengfen); //省份
            area.setCity(chengshi); //城市
            area.setDistrict(quyu);//区域
            area.setPostcode(youbian);//邮编

            String hanziPinyin = PinYin4jUtils.hanziToPinyin(chengshi, "");
            area.setCitycode(hanziPinyin);//城市编码

            //获取简码
            String str = getJianMa(shengfen, chengshi, quyu);

            //简码
            area.setShortcode(str);

            areas.add(area);
        }

        //调用service层
        String respon = areaService.areaImport(areas);

        return respon;

    }

    /**
     * 简码工具类  因为导入excel和增加或修改都需要用到简码,所以方法就抽取出来了
     * @param shengfen
     * @param chengshi
     * @param quyu
     * @return
     */
    private String getJianMa(String shengfen, String chengshi, String quyu) {
        //汉字首字母
        String[] SF = PinYin4jUtils.getHeadByString(shengfen);  //省份首字母
        //得到省份首字母
        String sfs = StringUtils.join(SF);
        //截掉最后一个字母
        String str1 = sfs.substring(0,sfs.length() - 1);  //省份

        String[] CS = PinYin4jUtils.getHeadByString(chengshi);  //城市首字母
        String css = StringUtils.join(CS);
        String str2 = css.substring(0,css.length() - 1);//城市

        String[] QY = PinYin4jUtils.getHeadByString(quyu);  //区域首字母
        String qys = StringUtils.join(QY);
        String str3 = qys.substring(0,qys.length() - 1);//区域

        //调用工具类将字母连接起来
        return str1 + str2 + str3;  //简码
    }

    /**
     * 初始化首页查询方法 / 复杂查询方法
     * @return
     */
    @RequestMapping("/area_pageQuery")
    @ResponseBody
    public Map areaPageQuery(int page, int rows, Area area){
        Map<String,Object> areaMap=new HashMap<>();

        Page<Area> areas = areaService.areaPageQuery(page,rows,area);

        areaMap.put("total",areas.getTotalElements());    //总条数
        areaMap.put("rows",areas.getContent());     //当前页数据

        return areaMap;
    }


    /**
     * 添加的方法
     * @param area
     * @return
     */
    @RequestMapping("/area_add")
    public String addArea(Area area){
        String chengshi = area.getCity();   //获取城市
        String hanziPinyin = PinYin4jUtils.hanziToPinyin(chengshi, "");
        area.setCitycode(hanziPinyin);//城市编码
        String shengfen = area.getProvince();//获取省份
        String quyu = area.getDistrict();//获取区域
        String str = this.getJianMa(shengfen, chengshi, quyu);
        area.setShortcode(str); //简码
        areaService.addArea(area);
        //添加完成之后重定向
        return "redirect:/pages/base/area.html";
    }


    /**
     * 删除的方法
     * @param ids
     * @return
     */
    @RequestMapping("/courier_delArea")
    public String delBatch(String ids) {
        areaService.delBatch(ids);
        return "redirect:/pages/base/area.html";
    }




}
